package com.orangelife.service.statistics.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.statistics.IStatisticsService;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.statistics.entity.StatisticsDto;

@Service("statisticsService")
public class StatisticsServiceImpl extends MybaitsGenericDaoSubLogic implements
		IStatisticsService {
	@Resource(name = "propFeeTypeService")
	private IPropFeeTypeService propFeeTypeService;

	@Override
	public List<Map<String, Object>> getFeeStatistics(StatisticsDto sta)
			throws AppBizException {

		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"StatisticsMapper.getFeeStatistics", sta);
	}

	@Override
	public List<Map<String, Object>> getFeeRateByDate(StatisticsDto sta)
			throws AppBizException {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"StatisticsMapper.getFeeStatisticsByDate", sta);
	}

	/* 获取缴费项目code和sql语句 */

	@Override
	public List<Map<String, Object>> getFeeTypeCode(StatisticsDto sta)
			throws AppBizException {
		if (StringUtils.isBlank(sta.getLevel())) {

			sta.setLevel("1");

		}

		List<Map<String, Object>> feeTypes = propFeeTypeService
				.getPropFeeTypeCodeByLevel(sta.getCommIDs(), sta.getCodes(),
						sta.getLevel());
		// List<Map<String,Object>> feeTypes=
		// propFeeTypeService.getPropFeeTypeCode(sta.getCommIDs());

		// Map<String,Object> mm=new HashMap<String,Object>();
		// String sqlstr="";
		// for (Map<String, Object> map2 : feeTypes) {
		// sqlstr+=" ,sum(case when t1.code='"+StringUtils.toString(map2.get("key"))+"' then round(t.amount, 1) ELSE 0 end  ) XM"+StringUtils.toString(map2.get("key"));
		//
		// }
		//
		//
		// mm.put("header", feeTypes);
		//
		// mm.put("sqlstr", sqlstr);
		return feeTypes;

	}

	@Override
	public List<Map<String, Object>> getOwnerBillList(StatisticsDto sta)
			throws AppBizException {
		// TODO Auto-generated method stub

		// 获取缴费类目
		List<Map<String, Object>> feeTypes = propFeeTypeService
				.getPropFeeType(sta.getCommIDs()[0]);
		List<Map<String, Object>> feeTypes1 = new ArrayList<Map<String, Object>>();
		String sqlstr = "";

		int n = 1;
		for (Map<String, Object> map : feeTypes) {
			if ("0".equals(StringUtils.toString(map.get("type")))) {

				sqlstr += " ,sum(case when t1.fee_type_id='"
						+ StringUtils.toString(map.get("feeTypeID"))
						+ "' then amount ELSE 0 end  ) XM" + n;

				map.put("key", n);
				feeTypes1.add(map);
				n++;
			}

		}
		System.out.println(feeTypes1 + "*************");

		sqlstr += " ,sum(case when t1.type='1' then amount ELSE 0 end  ) XM"
				+ n;

		Map<String, Object> mm = new HashMap<String, Object>();

		mm.put("fee_type_name", "公摊水电费");
		mm.put("key", n);

		feeTypes1.add(mm);
		Map<String, Object> param = new HashMap<String, Object>();
		System.out.println(sqlstr + "****************");
		param.put("sqlstr", sqlstr);

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		return this.getSqlSession().selectList(
				"StatisticsMapper.getOwerBillInfo", param);

	}

	@Override
	public Map<String, Object> getOwnerBillInfo(StatisticsDto sta)
			throws AppBizException {
		List<Map<String, Object>> feeTypes = propFeeTypeService
				.getPropFeeType(sta.getCommIDs()[0]);
		List<Map<String, Object>> feeTypes1 = new ArrayList<Map<String, Object>>();
		String sqlstr = "";

		int n = 1;
		for (Map<String, Object> map : feeTypes) {
			if ("0".equals(StringUtils.toString(map.get("type")))) {

				sqlstr += " ,sum(case when t1.fee_type_id='"
						+ StringUtils.toString(map.get("feeTypeID"))
						+ "' then amount ELSE 0 end  ) XM" + n;

				map.put("key", n);
				feeTypes1.add(map);
				n++;
			}

		}

		sqlstr += " ,sum(case when t1.type='1' then amount ELSE 0 end  ) XM"
				+ n;

		Map<String, Object> mm = new HashMap<String, Object>();

		mm.put("feeTypeName", "公摊水电费");
		mm.put("key", n);

		feeTypes1.add(mm);
		Map<String, Object> param = new HashMap<String, Object>();
		System.out.println(sqlstr + "****************");
		param.put("sqlstr", sqlstr);

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());

		String limit = "";
		int start = 0;

		int pageSize = sta.getPageSize();
		if (pageSize <= 0) {

			pageSize = 10;
		}

		if (sta.getPageIndex() >= 1) {

			start = (sta.getPageIndex() - 1) * pageSize;
		}
		limit += start + "," + pageSize;
		param.put("limit", limit);
		param.put("status", "0");

		System.out.println(limit + "****************");

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getOwerBillInfo", param);
		Map<String, Object> res1 = new HashMap<String, Object>();
		res1.put("header", feeTypes1);
		res1.put("list", res);
		return res1;

	}

	@Override
	public List<Map<String, Object>> paybyMon(StatisticsDto sta)
			throws AppBizException {

		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));
		param.put("codes", sta.getCodes());
		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("ismon", 1);
		param.put("status", sta.getStatus());
		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getPayMon", param);
		Map<String, Object> res1 = new HashMap<String, Object>();

		return res;

	}

	@Override
	public Map<String, Object> paybyMonAll(StatisticsDto sta)
			throws AppBizException {
		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));
		param.put("codes", sta.getCodes());
		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("isInvoice", sta.getIsInvoice());
		param.put("invoiceNum", sta.getInvoiceNum());

		param.put("ismon", sta.getIsall());

		if (sta.getPayTypes() != null && sta.getPayTypes().length > 0) {

			param.put("payTypes", sta.getPayTypes());
		}

		param.put("ownerName", sta.getOwnerName());
		param.put("status", sta.getStatus());
		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getPayMon", param);
		Map<String, Object> res1 = new HashMap<String, Object>();

		res1.put("header", feeTypes.get("header"));
		res1.put("list", res);
		return res1;

		// List<Map<String,Object>> res=
		// this.getSqlSession().selectList("StatisticsMapper.getPayMon", param);
		// Map <String,Object> res1=new HashMap<String,Object>();
		// res1.put("header", feeTypes1);
		// res1.put("list", res);
		// return res1;

	}

	@Override
	public Map<String, Object> getCommOweInfoAll(StatisticsDto sta, String isall)
			throws AppBizException {
		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));

		param.put("commIDs", sta.getCommIDs());
		if (sta.getCodes() != null && sta.getCodes().length > 0) {

			param.put("codes", sta.getCodes());

		}
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("ismon", sta.getIsall());
		param.put("orderBy", sta.getOrderBy());
		param.put("status", sta.getStatus());
		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getCommOweInfo", param);
		Map<String, Object> res1 = new HashMap<String, Object>();

		res1.put("header", feeTypes.get("header"));
		res1.put("list", res);
		return res1;
	}

	@Override
	public List<Map<String, Object>> getCommOweInfo(StatisticsDto sta,
			String isall) throws AppBizException {

		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("ismon", isall);
		if (sta.getCodes() != null && sta.getCodes().length > 0) {

			param.put("codes", sta.getCodes());

		}
		param.put("orderBy", sta.getOrderBy());

		param.put("status", sta.getStatus());
		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getCommOweInfo", param);
		return res;

	}

	@Override
	public List<Map<String, Object>> getOwerOweFeeInfo(StatisticsDto sta,
			String isall) throws AppBizException {

		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("level", sta.getLevel());
		param.put("ownerName", sta.getOwnerName());
		param.put("isall", isall);

		param.put("startAging", sta.getStartAging());
		param.put("st", sta.getSt());
		param.put("endAging", sta.getEndAging());
		param.put("et", sta.getEt());

		param.put("status", "0");
		if (sta.getCodes() != null && sta.getCodes().length > 0) {

			param.put("codes", sta.getCodes());

		}
		if (sta.getBulidingIDs() != null && sta.getBulidingIDs().length > 0) {

			param.put("bulidingIDs", sta.getBulidingIDs());

		}

		if (sta.getApartmentTypes() != null
				&& sta.getApartmentTypes().length > 0) {

			param.put("apartmentTypes", sta.getApartmentTypes());

		}

		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));
		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getOwerOweFeeInfo", param);
		return res;

	}

	@Override
	public Map<String, Object> getOwerOweFeeInfoAll(StatisticsDto sta,
			String isall) throws AppBizException {

		Map<String, Object> feeTypes = this.getFeeTypeCodeAndSql(sta, null,
				null);

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("level", sta.getLevel());
		param.put("ownerName", sta.getOwnerName());
		param.put("isall", sta.getIsall());
		param.put("status", "0");

		param.put("startAging", sta.getStartAging());
		param.put("st", sta.getSt());
		param.put("endAging", sta.getEndAging());
		param.put("et", sta.getEt());

		if (sta.getCodes() != null && sta.getCodes().length > 0) {

			param.put("codes", sta.getCodes());

		}
		if (sta.getBulidingIDs() != null && sta.getBulidingIDs().length > 0) {

			param.put("bulidingIDs", sta.getBulidingIDs());

		}

		if (sta.getApartmentTypes() != null
				&& sta.getApartmentTypes().length > 0) {

			param.put("apartmentTypes", sta.getApartmentTypes());

		}

		int pageSize = sta.getPageSize();
		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
				Integer.valueOf(pageSize));
		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getOwerOweFeeInfo", param);

		Map<String, Object> res1 = new HashMap<String, Object>();
		res1.put("header", feeTypes.get("header"));
		res1.put("list", res);
		return res1;
	}

	@Override
	public List<Map<String, Object>> getCommOweInfoDetail(StatisticsDto sta)
			throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();
		if (sta.getCodes() != null && sta.getCodes().length > 0) {
			param.put("codes", sta.getCodes());
		}
		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("isall", sta.getIsall());

		// int pageSize=sta.getPageSize();

		param.put("status", sta.getStatus());

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getCommOweInfoDetail", param);

		return res;
	}

	@Override
	public Map<String, Object> getFeeTypeCodeAndSql(StatisticsDto sta,
			String t, String t2) throws AppBizException {

		if (StringUtils.isBlank(sta.getLevel())) {

			sta.setLevel("1");

		}

		List<Map<String, Object>> feeTypes = propFeeTypeService
				.getPropFeeTypeCodeByLevel(sta.getCommIDs(), sta.getCodes(),
						sta.getLevel());

		Map<String, Object> mm = new HashMap<String, Object>();
		String sqlstr = "";

		String tb1 = !StringUtils.isBlank(t) ? t : "t1";
		String tb2 = !StringUtils.isBlank(t2) ? t2 : "t";
		if ("1".equals(sta.getLevel())) {

			for (Map<String, Object> map2 : feeTypes) {
				sqlstr += " ,sum(case when " + tb1 + ".code='"
						+ StringUtils.toString(map2.get("code")) + "' then "
						+ tb2 + ".amount ELSE 0 end  ) XM"
						+ StringUtils.toString(map2.get("code"));

			}

		} else if ("2".equals(sta.getLevel())) {

			for (Map<String, Object> map2 : feeTypes) {
				sqlstr += " ,sum(case when " + tb1 + ".s_code='"
						+ StringUtils.toString(map2.get("code")) + "' then "
						+ tb2 + ".amount ELSE 0 end  ) XM"
						+ StringUtils.toString(map2.get("code"));

			}

		} else {

			for (Map<String, Object> map2 : feeTypes) {
				sqlstr += " ,sum(case when " + tb1 + ".fee_type_id='"
						+ StringUtils.toString(map2.get("code")) + "' then "
						+ tb2 + ".amount ELSE 0 end  ) 'XM"
						+ StringUtils.toString(map2.get("code") + "'");

			}

		}

		mm.put("header", feeTypes);

		mm.put("sqlstr", sqlstr);
		return mm;
	}

	@Override
	public List<Map<String, Object>> paybillDetail(StatisticsDto sta)
			throws AppBizException {
		Map<String, Object> feeTypes = this
				.getFeeTypeCodeAndSql(sta, "h", "t1");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("sqlstr", feeTypes.get("sqlstr"));

		param.put("isInvoice", sta.getIsInvoice());
		param.put("invoiceNum", sta.getInvoiceNum());
		param.put("codes", sta.getCodes());
		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		if (sta.getPayTypes() != null && sta.getPayTypes().length > 0) {

			param.put("payTypes", sta.getPayTypes());
		}
		param.put("ownerName", sta.getOwnerName());

		param.put("apartmentNum", sta.getApartmentNum());

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getPayBillDetail", param);
		return res;
	}

	@Override
	public List<Map<String, Object>> getMonthPay(String commID,
			String apartmentNum, String apartmentType, String startDate,
			String endDate, int pageIndex, int pageSize) throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", commID);

		param.put("apartmentNum", apartmentNum);

		param.put("apartmentType", apartmentType);

		param.put("startDate", startDate);

		param.put("endDate", endDate);

		// param.put("pageIndex",pageIndex);
		//
		// param.put("pageSize",pageSize);

		PageHelper.startPage(pageIndex, pageSize);
		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"StatisticsMapper.getMonthPay", param);
		
		return res;
	}

	@Override
	public Map<String, Object> findDetail(String apartmentID, String feeDate)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("apartmentID", apartmentID);

		param.put("feeDate", feeDate);

		Map<String, Object> res = this.getSqlSession().selectOne(
				"StatisticsMapper.findDetail", param);

		return res;
	}
}
